package weaver.interfaces.zhlx.duss.action.purchasemodule.sap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SapUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 潜在供方准入批准书流程审批结束调用SAP创建供应商主数据接口
 * @ModifyLog:
 **/
public class WeaQzgfzrpzslcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        int formid = Math.abs(requestInfo.getRequestManager().getFormid());
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaQzgfzrpzslcAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SAP系统接口返回:";
            RecordSet rs = new RecordSet();
            SapUtil saptil = new SapUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            JSONObject dataJson = new JSONObject();
            JSONObject dataheaderJson = new JSONObject();
            dataheaderJson.put("SERIAL", requestid);
            dataheaderJson.put("OANUM", Util.null2String(rs.getString("bgbh")));
            dataheaderJson.put("SORT1", "");
            dataheaderJson.put("SORT2", Util.null2String(rs.getString("gysjc1")));
            dataheaderJson.put("LIFNR", "");
            String ywhbfz = Util.null2String(rs.getString("ywhbfz"));
            ywhbfz = saptil.getSelectname(formid, "ywhbfz", ywhbfz);
            dataheaderJson.put("KTOKK", ywhbfz);
            dataheaderJson.put("NAME1", Util.null2String(rs.getString("gysmc1")));
            dataheaderJson.put("NAME2", "");
            dataheaderJson.put("STRAS", Util.null2String(rs.getString("gfdz")));
            dataheaderJson.put("PSTLZ", "");
            dataheaderJson.put("LAND1", Util.null2String(rs.getString("gj")));
            dataheaderJson.put("REGIO", Util.null2String(rs.getString("dqdmdx")));
            dataheaderJson.put("LANGU", Util.null2String(rs.getString("yydm")));
            dataheaderJson.put("TELF1", "");
            dataheaderJson.put("TELFX", Util.null2String(rs.getString("cz")));
            dataheaderJson.put("TELF2", Util.null2String(rs.getString("lxfs")));
            dataheaderJson.put("SMTP_ADDR", Util.null2String(rs.getString("dzyjdz")));
            dataheaderJson.put("IDNUMBER", Util.null2String(rs.getString("wybsbh")));
            dataheaderJson.put("ALTKN", "");
            dataheaderJson.put("VBUND", Util.null2String(rs.getString("myhb")));
            String wybslx = Util.null2String(rs.getString("wybslx"));
            wybslx = saptil.getSelectname(formid, "wybslx", wybslx);
            dataheaderJson.put("IDTYPE", wybslx);
            dataJson.put("DATAHEADER", dataheaderJson);
            JSONArray dataitem = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt2 where mainid=?", mainid);
            while (rs.next()) {
                JSONObject dataitemJson = new JSONObject();
                dataitemJson.put("BKVID", Util.null2String(rs.getString("yxxh")));
                dataitemJson.put("BANKS", Util.null2String(rs.getString("yxgj")));
                dataitemJson.put("BANKL", Util.null2String(rs.getString("yxzhdm")));
                dataitemJson.put("BANKA", Util.null2String(rs.getString("yxmc")));
                dataitemJson.put("PROVZ", Util.null2String(rs.getString("yxdq")));
                dataitemJson.put("STRAS", Util.null2String(rs.getString("jd")));
                dataitemJson.put("ORT01", Util.null2String(rs.getString("cs")));
                dataitemJson.put("BRNCH", Util.null2String(rs.getString("fx")));
                dataitemJson.put("BKEXT", Util.null2String(rs.getString("yxbz")));
                dataitemJson.put("BKREF", "");
                dataitemJson.put("BANKN", Util.null2String(rs.getString("yxzh")));
                dataitemJson.put("SWIFT", Util.null2String(rs.getString("swiftcode")));
                String kzm = Util.null2String(rs.getString("kzm"));
                kzm = saptil.getSelectname(formid, "kzm", kzm);
                dataitemJson.put("BKONT", kzm);
                dataitemJson.put("IBAN", Util.null2String(rs.getString("gjyxzh")));
                dataitemJson.put("KOINH", Util.null2String(rs.getString("yxhzsj")));
                dataitemJson.put("BK_VALID_FROM", Util.null2String(rs.getString("yxqksrq")).replace("-", ""));
                dataitemJson.put("BK_VALID_TO", Util.null2String(rs.getString("yxqjsrq")).replace("-", ""));
                dataitem.add(dataitemJson);
            }
            dataJson.put("DATAITEM", dataitem);

            JSONArray puritem = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt3 where mainid=?", mainid);
            while (rs.next()) {
                JSONObject puritemJson = new JSONObject();
                puritemJson.put("EKORG", Util.null2String(rs.getString("cgzz")));
                puritemJson.put("WAERS", Util.null2String(rs.getString("hbm")));
                puritemJson.put("ZTERM", Util.null2String(rs.getString("fktjdm")));
                puritemJson.put("VERKF", Util.null2String(rs.getString("xsry")));
                puritemJson.put("TELF1", Util.null2String(rs.getString("gysdhhm")));
                puritemJson.put("INCO1", Util.null2String(rs.getString("gjmytk")));
                puritemJson.put("INCO2", Util.null2String(rs.getString("gjmytkbf2")));
                puritemJson.put("SPERM", Util.null2String(rs.getString("dj")));
                puritem.add(puritemJson);
            }
            dataJson.put("PURITEM", puritem);

            JSONArray fiitem = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt4 where mainid=?", mainid);
            while (rs.next()) {
                JSONObject fiitemJson = new JSONObject();
                fiitemJson.put("BUKRS", Util.null2String(rs.getString("gsdm")));
                fiitemJson.put("AKONT", Util.null2String(rs.getString("tykm")));
                fiitemJson.put("ZUAWA", Util.null2String(rs.getString("pxm")));
                fiitemJson.put("ZTERM", Util.null2String(rs.getString("fktj")));
                fiitemJson.put("ZWELS", Util.null2String(rs.getString("fkfs")));
                fiitemJson.put("TOGRU", "");
                fiitem.add(fiitemJson);
            }
            dataJson.put("FIITEM", fiitem);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaQzgfzrpzslcAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            writeLog(">>>>>WeaQzgfzrpzslcAction>>>>>dataJson=" + dataJson);
            JSONObject resultJson = saptil.handleOKHttpRequest("MM001", dataJson, dataMap);
            writeLog(">>>>>WeaQzgfzrpzslcAction>>>>>resultJson=" + resultJson);
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                String data = Util.null2String(outputJson.getString("DATA"));
                JSONObject retrundataJson = JSONObject.parseObject(data);
                String returnStr = retrundataJson.getString("RETURN");
                JSONArray returnList = JSONArray.parseArray(returnStr);
                if ("S".equals(msgType)) {
                    for (int i = 0; i < returnList.size(); i++) {
                        JSONObject returnJson = returnList.getJSONObject(i);
                        String lifnr = Util.null2String(returnJson.getString("LIFNR"));
                        rs.executeUpdate("update " + tablename + " set gysbh=? where requestid=?", lifnr, requestid);
                    }
                } else {
                    for (int i = 0; i < returnList.size(); i++) {
                        String msgType1 = returnList.getJSONObject(i).getString("MSG_TYPE");
                        if ("E".equalsIgnoreCase(msgType1)) {
                            String msgText2 = returnList.getJSONObject(i).getString("MSG_TEXT");
                            message += msgText2 + ";";
                        }
                    }
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaQzgfzrpzslcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}